//
//  DeviceTabBarController.m
//  AlinkDemo
//
//  Created by Vincent on 2017/10/28.
//  Copyright © 2017年 aliyun. All rights reserved.
//

#import "DeviceTabBarController.h"
#import "DeviceDetialTableViewController.h"
#import "VisitorViewController.h"
#import "CaptureViewController.h"
@interface DeviceTabBarController ()<UITabBarDelegate>

@end

@implementation DeviceTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBar.tintColor = UIColorFromRGB(0x02C7B2);

    NSString *nick = self.deviceDict[@"nick"];
    self.title =nick?nick:self.deviceDict[@"name"];

    DeviceDetialTableViewController *detailVc = self.viewControllers[0];
    detailVc.deviceDict = self.deviceDict;

    VisitorViewController *alarmVc = self.viewControllers[1];
    alarmVc.isAlarm = YES;
    alarmVc.deviceDict = self.deviceDict;
    
    VisitorViewController *visitorVc = self.viewControllers[2];
    visitorVc.deviceDict = self.deviceDict;
    visitorVc.isAlarm = NO;

    CaptureViewController *captureVc = self.viewControllers[3];
    captureVc.deviceDict = self.deviceDict;

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTitle) name:@"UpdateTitleNotification" object:nil];

}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{


    if ([item.title isEqualToString:@"报警消息"]||[item.title isEqualToString:@"访客记录"]||[item.title isEqualToString:@"抓拍记录"]) {
        self.title = item.title;

    }else{
        NSArray *deviceList = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceList"];
        if (deviceList) {

            for (NSDictionary *device in deviceList) {

                if ([self.deviceDict[@"bid"] isEqualToString:device[@"bid"]]) {
                    NSString *nick = device[@"nick"];
                    self.title =nick?nick:self.deviceDict[@"name"];
                    break;
                }
            }
        }

    }



}
-(void)updateTitle{
    if (self.selectedIndex==0) {
        NSArray *deviceList = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceList"];
        if (deviceList) {
            
            for (NSDictionary *device in deviceList) {
                
                if ([self.deviceDict[@"bid"] isEqualToString:device[@"bid"]]) {
                    NSString *nick = device[@"nick"];
                    self.title =nick?nick:self.deviceDict[@"name"];
                    break;
                }
            }
        }

    }
   
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
